package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Service(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Autowired
    public SetmealMapper setmealMapper;

    @Autowired
    private JedisPool jedisPool;

    @Override
    public void add(Integer[] checkItemIds, CheckGroup checkGroup) {

        //新增数据插入到t_checkgroup
        checkGroupMapper.add(checkGroup);

        //新增t_checkgroup_checkitem数据
        setCheckGroupAndCheckItem(checkItemIds,checkGroup.getId());

    }

    private void setCheckGroupAndCheckItem(Integer[] checkItemIds, Integer id) {

        if (checkItemIds!=null && checkItemIds.length>0){
            //将添加检查组和检查项的中间表数据
            checkGroupMapper.setCheckGroupAndCheckItem(checkItemIds,id);

        }

    }


    /*private void setCheckGroupAndCheckItem(Integer[] checkItemIds, Integer id) {

        if (checkItemIds!=null && checkItemIds.length>0){

            //将添加检查组和检查项的中间表数据
            for (Integer checkItemId : checkItemIds) {
                checkGroupMapper.setCheckGroupAndCheckItem(checkItemId,id);
            }

        }


    }*/

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        //定义页面大小和当前页的页码
        PageHelper.startPage(currentPage,pageSize);
        //条件查询检查组信息
        List<CheckGroup> list = checkGroupMapper.findByCondition(queryString);
        //包装数据
        PageInfo<CheckGroup> pageInfo = new PageInfo<>(list);

        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 通过id查询检查组
     * @param id
     * @return
     */
    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupMapper.findById(id);
    }

    /**
     * 检查组编辑更新
     * @param checkItemIds
     * @param checkGroup
     */
    @Override
    public void edit(Integer[] checkItemIds, CheckGroup checkGroup) {

        //通过检查组的id删除检查项
        checkGroupMapper.deleteAssociation(checkGroup.getId());
        //重新插入中间表数据
        setCheckGroupAndCheckItem(checkItemIds,checkGroup.getId());
        //更新检查组数据
        checkGroupMapper.edit(checkGroup);
        //查询和此检查组有关的套餐，并将缓存内的套餐细节删除
        List<Setmeal> list = setmealMapper.findByCheckGroup(checkGroup.getId());
        for (Setmeal setmeal : list) {
            jedisPool.getResource().del(setmeal.getId()+ RedisMessageConstant.SETMEAL_DETAIL);
        }
    }

    /**
     * 通过id删除检查组
     * @param id
     */
    @Override
    public void deleteById(Integer id) {

        //查询当前检查组是否被套餐引用
        Integer count = checkGroupMapper.findCountById(id);
        if (count>0){
            //抛出异常，提示错误信息
            throw new RuntimeException(MessageConstant.CHECKGROUP_IS_QUOTED);
        }

        //删除检查组和检查项中间表数据
        checkGroupMapper.deleteAssociation(id);

        //删除检查组
        checkGroupMapper.deleteById(id);

    }



    /**
     * 查询所有检查组信息
     * @return
     */
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupMapper.findAll();
    }
}
